export const componentPlugin = async (app) => {
	// 导入所有
	const modulesType = import.meta.glob('./plugin/**/*type.js')
	const modulesComponent = import.meta.glob('./plugin/**/*config.js')
	const pesetsList = []
	const jsonDataType = []
	const jsonDataComponent = []
	// 获取所有大类json
	for (const [path, module] of Object.entries(modulesType)) {
		;(async () => {
			const data = await module()
			jsonDataType.push(data.default)
		})()
	}

	// 获取所有组件json 包括attrform
	for (const [path, module] of Object.entries(modulesComponent)) {
		;(async () => {
			const data = await module()
			jsonDataComponent.push(data.default)
			// 注册所有组件
			const component = await import(`./plugin/components/${data.default.type}/index.vue`)
			app.component(data.default.name, component.default)
			// 注册所有组件的样式表单
			try {
				const styleForm = await import(`./plugin/components/${data.default.type}/styleForm/index.vue`)
				app.component(data.default.name + 'StyleForm', styleForm.default)
			} catch (error) {
				console.log(error)
			}
			// 预设样式
			try {
				const styleListForm = await import(`./plugin/components/${data.default.type}/styleForm/pesets.js`)
				pesetsList.push({
					type: data.default.type,
					name: data.default.name,
					pesetsList: [data.default.style, ...styleListForm.default]
				})
			} catch (error) {
				pesetsList.push({ type: data.default.type, name: data.default.name, pesetsList: [data.default.style] })
			}
			// 注册所有组件的数据表单
			try {
				const attrform = await import(`./plugin/components/${data.default.type}/dataForm/index.vue`)
				app.component(data.default.name + 'DataForm', attrform.default)
			} catch (error) {}
		})()
	}
	//  pinia
	;(async () => {
		const pinia = await import(`@/views/plugin/dashboard/store`)
		const useDesignStore = pinia.designStore()

		// 存储所有组件的类别type.json
		useDesignStore.pesetsList = pesetsList
		// 存储所有组件的config.js
		useDesignStore.componentsConfigList = jsonDataComponent.sort((a, b) => a.sort - b.sort)
	})()
}
